package com.net.xpay.web.controller.merchant.collect;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.core.CoreProperties;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.collect.CollectionOrder;
import com.net.xpay.core.helper.QrCodeHelper;
import com.net.xpay.core.manager.collect.CollectionOrderManager;
import com.net.xpay.core.model.CollectionOrderSearchModel;
import com.net.xpay.core.model.CollectionOrderTransModel;
import com.net.xpay.core.service.CollectionOrderService;
import com.net.xpay.core.utils.UuidUtil;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/merchant/collection-order")
@Api(tags = "商户/收集收款信息", description = "商户 收集收款信息 net")
public class CollectionOrderController {

    @Autowired
    private UserManager userManager;

    @Autowired
    private CollectionOrderManager collectionOrderManager;

    @Autowired
    private QrCodeHelper qrCodeHelper;

    @Autowired
    private CollectionOrderService collectionOrderService;

    @Autowired
    private CoreProperties coreProperties;

    /**
     * 获取收款二维码
     */
    @ApiOperation("获取收款二维码")
    @GetMapping("/get-qrcode")
    public ResultData<String> getQrcode(@ApiIgnore @RequestUser User merchant) {
        if (!Strings.isNullOrEmpty(merchant.getCollectQrcode())) {
            return new ResultData<>(merchant.getCollectQrcode());
        }
        if (Strings.isNullOrEmpty(merchant.getUid())) {
            merchant.setUid(UuidUtil.uuid());
        }
        String url = "http://" + coreProperties.getMobileDomain() + "/#/collectOrder?uid=" + merchant.getUid();
        try {
            String qrcode = qrCodeHelper.genThenUpload(url);
            merchant.setCollectQrcode(qrcode);
            userManager.updateByPrimaryKeyWithVersion(merchant);
            return new ResultData<>(qrcode);
        } catch (IOException e) {
            log.error("genQrcode error userId={}", merchant.getId(), e);
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "系统繁忙");
        }
    }

    /**
     * 搜索
     */
    @ApiOperation("搜索")
    @PostMapping("/search")
    public ResultPagedList<CollectionOrder> search(@ApiIgnore @RequestUser User merchant, @RequestBody CollectionOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);

        searchModel.setUserId(merchant.getId());
        List<CollectionOrder> records = collectionOrderManager.search(searchModel);
        long count = collectionOrderManager.searchCount(searchModel);

        return new ResultPagedList<>(records, count, searchModel);
    }


    /**
     * 发起转账
     */
    @ApiOperation("发起转账")
    @PostMapping("/trans")
    public ResultData<String> trans(@ApiIgnore @RequestUser User merchant, @RequestBody CollectionOrderTransModel transModel) {
        log.info("trans collectionOrders transModel={}|userId={}", SerializationUtil.obj2String(transModel), merchant.getId());

        String payUrl = collectionOrderService.trans(merchant, transModel);
        return new ResultData<>(payUrl);
    }


}
